package com.kucai.yifaqapp.ui.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.pickerview.OptionsPickerView;
import com.kucai.yifaqapp.R;
import com.kucai.yifaqapp.global.HttpAdress;
import com.kucai.yifaqapp.global.Info;
import com.kucai.yifaqapp.model.CustomObserver;
import com.kucai.yifaqapp.model.network.HttpJsonUtils;
import com.kucai.yifaqapp.model.network.entity.res.Pay_BANKLISTBEAN;
import com.kucai.yifaqapp.model.network.entity.res.Pay_BIndCardReslut;
import com.kucai.yifaqapp.model.network.entity.res.Pay_BankCard;
import com.kucai.yifaqapp.util.SYSOUtils;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class BankidActivity extends BaseActivitySimple {
    private LinearLayout skinbg;
    private RelativeLayout back;
    private RelativeLayout unbind;//未绑定
    private RelativeLayout binding;//绑定
    private RelativeLayout binded;
    private RelativeLayout changebind;
    private OptionsPickerView pvOptions;

    //绑定卡的空间
    private TextView banknumber;
    private List<String> mdatas = new ArrayList<>();
    private Pay_BANKLISTBEAN payBanklistbean;
    private int bank_id;
    private Button conform;
    private EditText wangdian;
    private EditText kahao;
    //绑定完成
    private ImageView bankname;
    private ImageView changebank;
    private TextView text001;
    private TextView text002;
    private TextView text003;
    private ImageView change;//更换银行卡

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bankid);
        back = findViewById(R.id.back);
        skinbg = findViewById(R.id.skinbg);
        unbind = findViewById(R.id.unbind);
        binding = findViewById(R.id.binding);
        binded = findViewById(R.id.binded);
        changebind = findViewById(R.id.changebind);
        conform = findViewById(R.id.conform);
        wangdian = findViewById(R.id.wangdianname);
        kahao = findViewById(R.id.kahao);
        banknumber = findViewById(R.id.banknumber);
        bankname = findViewById(R.id.bankname);
        changebank = findViewById(R.id.changebank);
        text001 = findViewById(R.id.text001);
        text002 = findViewById(R.id.text002);
        text003 = findViewById(R.id.text003);
        change = findViewById(R.id.change);
        initstates();
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initstates() {
        HttpJsonUtils.getHttpApiInstance()
                .getCardNumber(HttpAdress.getuseAdress() + "api/m/user/getUserBank",Info.user.token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<Pay_BankCard>(this) {
                    @Override
                    protected void onCustomNext(Pay_BankCard resBanner) {
                        try {
                            if (resBanner.getData().getBankName() == null) {
                                //开始绑卡
                                unbind.setVisibility(View.VISIBLE);
                                binding.setVisibility(View.GONE);
                                binded.setVisibility(View.GONE);
                                changebind.setVisibility(View.GONE);
                                unbind.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        HttpJsonUtils.getHttpApiInstance()
                                                .getBankList(HttpAdress.getuseAdress() + "api/m/user/getBanks",Info.user.token)
                                                .subscribeOn(Schedulers.io())
                                                .observeOn(AndroidSchedulers.mainThread())
                                                .subscribe(new CustomObserver<Pay_BANKLISTBEAN>(BankidActivity.this) {
                                                    @Override
                                                    protected void onCustomNext(Pay_BANKLISTBEAN resBanner) {
                                                        try {
                                                            if (resBanner.getCode() == 0) {
                                                                payBanklistbean = resBanner;
                                                                for (int i = 0; i < resBanner.getData().size(); i++) {
                                                                    mdatas.add(resBanner.getData().get(i).getName());
                                                                }
                                                                unbind.setVisibility(View.GONE);
                                                                binding.setVisibility(View.VISIBLE);
                                                                binded.setVisibility(View.GONE);
                                                                changebind.setVisibility(View.GONE);
                                                                pvOptions = new OptionsPickerView.Builder(BankidActivity.this, new OptionsPickerView.OnOptionsSelectListener() {
                                                                    @Override
                                                                    public void onOptionsSelect(int options1, int option2, int options3, View v) {
                                                                        banknumber.setText(mdatas.get(options1));
                                                                        bank_id = options1 + 1;
                                                                    }
                                                                })
                                                                        .setSubCalSize(20)//确定和取消文字大小
                                                                        .setSubmitColor(Color.BLUE)//确定按钮文字颜色
                                                                        .setCancelColor(Color.BLUE)//取消按钮文字颜色
                                                                        .setTextColorCenter(Color.BLACK)//设置选中项的颜色
                                                                        .build();
                                                                pvOptions.setPicker(mdatas);
                                                                banknumber.setOnClickListener(new View.OnClickListener() {
                                                                    @Override
                                                                    public void onClick(View v) {
                                                                        pvOptions.show();
                                                                    }
                                                                });
                                                                conform.setOnClickListener(new View.OnClickListener() {
                                                                    @Override
                                                                    public void onClick(View v) {
                                                                        if (wangdian.getText().toString().isEmpty()) {
                                                                            Toast.makeText(BankidActivity.this, "请输入网点", Toast.LENGTH_SHORT).show();
                                                                            return;
                                                                        }
                                                                        if (kahao.getText().toString().isEmpty()) {
                                                                            Toast.makeText(BankidActivity.this, "请输入卡号", Toast.LENGTH_SHORT).show();
                                                                            return;
                                                                        }
                                                                        //开始提交银行数据
                                                                        HttpJsonUtils.getHttpApiInstance()
                                                                                .getBandingcard( HttpAdress.getuseAdress() + "api/m/user/bindBank",Info.user.token, bank_id, kahao.getText().toString(), wangdian.getText().toString())
                                                                                .subscribeOn(Schedulers.io())
                                                                                .observeOn(AndroidSchedulers.mainThread())
                                                                                .subscribe(new CustomObserver<Pay_BIndCardReslut>(BankidActivity.this) {
                                                                                    @Override
                                                                                    protected void onCustomNext(Pay_BIndCardReslut resBanner) {
                                                                                        try {
                                                                                            if (resBanner.getCode() == 0) {
                                                                                                Toast.makeText(BankidActivity.this, "绑定成功", Toast.LENGTH_SHORT).show();
                                                                                                initstates();
                                                                                            } else {
                                                                                                Toast.makeText(BankidActivity.this, resBanner.getMsg(), Toast.LENGTH_SHORT).show();
                                                                                            }
                                                                                        } catch (Exception e) {
                                                                                            Toast.makeText(BankidActivity.this, "异常错误，请重新操作", Toast.LENGTH_SHORT).show();
                                                                                        }
                                                                                    }

                                                                                    @Override
                                                                                    protected void onSubscribeCustom(Disposable disposable) {
                                                                                    }

                                                                                    @Override
                                                                                    protected void onErrorCustom(Throwable e) {
                                                                                        SYSOUtils.sOut(e.toString());
                                                                                    }
                                                                                });
                                                                    }
                                                                });
                                                            } else {
                                                                Toast.makeText(BankidActivity.this, resBanner.getMsg(), Toast.LENGTH_LONG).show();
                                                            }
                                                        } catch (Exception e) {
                                                            Toast.makeText(BankidActivity.this, "异常错误，请重新操作", Toast.LENGTH_LONG).show();
                                                        }
                                                    }

                                                    @Override
                                                    protected void onSubscribeCustom(Disposable disposable) {
                                                    }

                                                    @Override
                                                    protected void onErrorCustom(Throwable e) {
                                                        SYSOUtils.sOut(e.toString());
                                                    }
                                                });
                                    }
                                });
                            } else {
                                //显示已经绑定的状态
                                unbind.setVisibility(View.GONE);
                                binding.setVisibility(View.GONE);
                                binded.setVisibility(View.VISIBLE);
                                changebind.setVisibility(View.GONE);
//                                中国农业银行--1
//                                中国建设银行--2
//                                中国工商银行--3
//                                招商银行--4
//                                交通银行--5
//                                中国民生银行--6
//                                兴业银行--7
//                                中国银行--8
//                                中国邮政储蓄银行--9
//                                光大银行--10
//                                中信银行--11
//                                广发银行--12
//                                浦发银行--13
//                                华夏银行--14
//                                平安银行--15
//                                北京银行--16
//                                上海银行--17
//                                北京农商银行--18
                                switch (resBanner.getData().getBank_id() + "") {
                                    case "1":
                                        bankname.setBackgroundResource(R.mipmap.ico_abc);
                                        break;
                                    case "2":
                                        bankname.setBackgroundResource(R.mipmap.ico_ccb);
                                        break;
                                    case "3":
                                        bankname.setBackgroundResource(R.mipmap.ico_icbc);
                                        break;
                                    case "4":
                                        bankname.setBackgroundResource(R.mipmap.ico_cmb);
                                        break;
                                    case "5":
                                        bankname.setBackgroundResource(R.mipmap.ico_bcm);
                                        break;
                                    case "6":
                                        bankname.setBackgroundResource(R.mipmap.ico_cmbc);
                                        break;
                                    case "7":
                                        bankname.setBackgroundResource(R.mipmap.ico_cib);
                                        break;
                                    case "8":
                                        bankname.setBackgroundResource(R.mipmap.ico_boc);
                                        break;
                                    case "9":
                                        bankname.setBackgroundResource(R.mipmap.ico_psbc);
                                        break;
                                    case "10":
                                        bankname.setBackgroundResource(R.mipmap.ico_ceb);
                                        break;
                                    case "11":
                                        bankname.setBackgroundResource(R.mipmap.ico_cncb);
                                        break;
                                    case "12":
                                        bankname.setBackgroundResource(R.mipmap.ico_cgb_1);
                                        break;
                                    case "13":
                                        bankname.setBackgroundResource(R.mipmap.ico_spdb);
                                        break;
                                    case "14":
                                        bankname.setBackgroundResource(R.mipmap.ico_hxb);
                                        break;
                                    case "15":
                                        bankname.setBackgroundResource(R.mipmap.ico_pingan);
                                        break;
                                    case "16":
                                        bankname.setBackgroundResource(R.mipmap.ico_bjyh);
                                        break;
                                    case "17":
                                        bankname.setBackgroundResource(R.mipmap.ico_bos);
                                        break;
                                    case "18":
                                        bankname.setBackgroundResource(R.mipmap.ico_bjns);
                                        break;
                                }
                                text001.setText("开户姓名：" + resBanner.getData().getFullName());
                                text002.setText("开户账号：" + resBanner.getData().getCardNo());
                                text003.setText("开户网点：" + resBanner.getData().getSubAddress());
                                change.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        //重新开始绑卡
                                        //开始绑卡
                                        unbind.setVisibility(View.VISIBLE);
                                        binding.setVisibility(View.GONE);
                                        binded.setVisibility(View.GONE);
                                        changebind.setVisibility(View.GONE);
                                        unbind.setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                HttpJsonUtils.getHttpApiInstance()
                                                        .getBankList(HttpAdress.getuseAdress() + "api/m/user/getBanks",Info.user.token)
                                                        .subscribeOn(Schedulers.io())
                                                        .observeOn(AndroidSchedulers.mainThread())
                                                        .subscribe(new CustomObserver<Pay_BANKLISTBEAN>(BankidActivity.this) {
                                                            @Override
                                                            protected void onCustomNext(Pay_BANKLISTBEAN resBanner) {
                                                                try {
                                                                    if (resBanner.getCode() == 0) {
                                                                        payBanklistbean = resBanner;
                                                                        for (int i = 0; i < resBanner.getData().size(); i++) {
                                                                            mdatas.add(resBanner.getData().get(i).getName());
                                                                        }
                                                                        unbind.setVisibility(View.GONE);
                                                                        binding.setVisibility(View.VISIBLE);
                                                                        binded.setVisibility(View.GONE);
                                                                        changebind.setVisibility(View.GONE);
                                                                        pvOptions = new OptionsPickerView.Builder(BankidActivity.this, new OptionsPickerView.OnOptionsSelectListener() {
                                                                            @Override
                                                                            public void onOptionsSelect(int options1, int option2, int options3, View v) {
                                                                                banknumber.setText(mdatas.get(options1));
                                                                                bank_id = options1 + 1;
                                                                            }
                                                                        })
                                                                                .setSubCalSize(20)//确定和取消文字大小
                                                                                .setSubmitColor(Color.BLUE)//确定按钮文字颜色
                                                                                .setCancelColor(Color.BLUE)//取消按钮文字颜色
                                                                                .setTextColorCenter(Color.BLACK)//设置选中项的颜色
                                                                                .build();
                                                                        pvOptions.setPicker(mdatas);
                                                                        banknumber.setOnClickListener(new View.OnClickListener() {
                                                                            @Override
                                                                            public void onClick(View v) {
                                                                                pvOptions.show();
                                                                            }
                                                                        });
                                                                        conform.setOnClickListener(new View.OnClickListener() {
                                                                            @Override
                                                                            public void onClick(View v) {
                                                                                if (wangdian.getText().toString().isEmpty()) {
                                                                                    Toast.makeText(BankidActivity.this, "请输入网点", Toast.LENGTH_SHORT).show();
                                                                                    return;
                                                                                }
                                                                                if (kahao.getText().toString().isEmpty()) {
                                                                                    Toast.makeText(BankidActivity.this, "请输入卡号", Toast.LENGTH_SHORT).show();
                                                                                    return;
                                                                                }
                                                                                //开始提交银行数据
                                                                                HttpJsonUtils.getHttpApiInstance()
                                                                                        .getBandingcard( HttpAdress.getuseAdress() + "api/m/user/bindBank",Info.user.token, bank_id, kahao.getText().toString(), wangdian.getText().toString())
                                                                                        .subscribeOn(Schedulers.io())
                                                                                        .observeOn(AndroidSchedulers.mainThread())
                                                                                        .subscribe(new CustomObserver<Pay_BIndCardReslut>(BankidActivity.this) {
                                                                                            @Override
                                                                                            protected void onCustomNext(Pay_BIndCardReslut resBanner) {
                                                                                                try {
                                                                                                    if (resBanner.getCode() == 0) {
                                                                                                        Toast.makeText(BankidActivity.this, "绑定成功", Toast.LENGTH_SHORT).show();
                                                                                                        initstates();
                                                                                                    } else {
                                                                                                        Toast.makeText(BankidActivity.this, resBanner.getMsg(), Toast.LENGTH_SHORT).show();
                                                                                                    }
                                                                                                } catch (Exception e) {
                                                                                                    Toast.makeText(BankidActivity.this, "异常错误，请重新操作", Toast.LENGTH_SHORT).show();
                                                                                                }
                                                                                            }

                                                                                            @Override
                                                                                            protected void onSubscribeCustom(Disposable disposable) {
                                                                                            }

                                                                                            @Override
                                                                                            protected void onErrorCustom(Throwable e) {
                                                                                                SYSOUtils.sOut(e.toString());
                                                                                            }
                                                                                        });
                                                                            }
                                                                        });
                                                                    } else {
                                                                        Toast.makeText(BankidActivity.this, resBanner.getMsg(), Toast.LENGTH_LONG).show();
                                                                    }
                                                                } catch (Exception e) {
                                                                    Toast.makeText(BankidActivity.this, "异常错误，请重新操作", Toast.LENGTH_LONG).show();
                                                                }
                                                            }

                                                            @Override
                                                            protected void onSubscribeCustom(Disposable disposable) {
                                                            }

                                                            @Override
                                                            protected void onErrorCustom(Throwable e) {
                                                                SYSOUtils.sOut(e.toString());
                                                            }
                                                        });
                                            }
                                        });
                                    }
                                });
                            }
                        } catch (Exception e) {

                        }
                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {
                        SYSOUtils.sOut(e.toString());
                    }
                });
    }

    @Override
    protected int setContentView() {
        return R.layout.activity_bankid;
    }

    @Override
    protected void ChangeSkin() {
        switch (SkinId) {
            case 17771:
                skinbg.setBackgroundResource(R.mipmap.skin_bg1);
                break;
            case 17772:
                skinbg.setBackgroundResource(R.mipmap.skin_bg2);
                break;
            case 17773:
                skinbg.setBackgroundResource(R.mipmap.skin_bg3);
                break;
            default:
                skinbg.setBackgroundResource(R.mipmap.skin_bg1);
        }
    }
}
